package com.tcat.user.web;

import com.tcat.common.vo.PageResult;
import com.tcat.user.pojo.Permissions;
import com.tcat.user.pojo.Roles;
import com.tcat.user.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "权限管理")
@RestController
@RequestMapping("permissions")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;


    /**
     * 分页查询权限
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param search
     * @return
     */
    @ApiOperation(value = "分页查询角色")
    @GetMapping("page")
    public ResponseEntity<PageResult<Permissions>> queryPermissionsByPage(
            @RequestParam(value="page",defaultValue = "1") Integer page,
            @RequestParam(value="rows",defaultValue = "5") Integer rows,
            @RequestParam(value="sortBy",required = false) String sortBy,
            @RequestParam(value="desc",defaultValue = "false") Boolean desc,
            @RequestParam(value="search",required = false) String  search
    ){
        return ResponseEntity.ok(permissionService.queryPermissionsByPage(page,rows,sortBy,desc,search));
    }


    /**
     * 删除tb_rps中的数据，单个删除、多个删除二合一
     * @param pid
     * @return
     */
    @DeleteMapping("pid/{permissions}")
    public ResponseEntity<Void> deletePermissions(@PathVariable("permissions") String pid){
        String separator="-";
        if(pid.contains(separator)){
            String[] ids=pid.split(separator);
            for(String id:ids){
                this.permissionService.deletePermissions(Long.parseLong(id));
            }
        }else{
            this.permissionService.deletePermissions(Long.parseLong(pid));
        }
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 删除tb_rps中的数据
     * @param pid
     * @return
     */
    @DeleteMapping("rid_pid/{permissions}")
    public ResponseEntity<Void> deleteByBrandIdInCategoryBrand(@PathVariable("permissions") Long pid){
        permissionService.deleteByPermissionInRps(pid);
        return ResponseEntity.status(HttpStatus.OK).build();
    }


    /**
     * 用于修改角色信息时，角色权限信息的回显
     * @param rid
     * @return
     */
    @ApiOperation(value = "用于修改品牌信息时，商品分类信息的回显")
    @GetMapping("rid/{rid}")
    public ResponseEntity<List<Permissions>> queryByRoleId(@PathVariable("rid") Long rid){
        List<Permissions> list=permissionService.queryByRoleId(rid);
        if(list == null || list.size()<1){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(list);
    }



    /**
     * 新增权限
     * @param permission
     * @return
     */
    @ApiOperation(value = "新增权限")
    @PostMapping
    public ResponseEntity<Void> savePermissions(Permissions permission){
        permissionService.savePermissions(permission);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 权限修改
     * @param permission
     * @return
     */
    @ApiOperation(value = "修改权限")
    @PutMapping
    public ResponseEntity<Void> updatePermissions(Permissions permission){
        this.permissionService.updatePermission(permission);
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
    }


    @ApiOperation(value="查询所有权限")
    @GetMapping("list")
    public ResponseEntity<List<Permissions>> queryAll(){
        return ResponseEntity.ok(permissionService.queryAll());
    }


}
